package com.yufubao.api.controller.applet.product;


import com.yufubao.framework.annotation.SystemLog;
import com.yufubao.framework.constant.NotesConstant;
import com.yufubao.framework.result.PageUtils;
import com.yufubao.framework.result.ResponseResult;
import com.yufubao.model.pojo.applet.product.AttrGroupEntity;
import com.yufubao.model.vo.applet.product.AttrAttrGroupRelationVo;
import com.yufubao.model.vo.applet.product.AttrVo;
import com.yufubao.service.service.applet.product.AttrGroupService;
import com.yufubao.service.service.applet.product.SupplierProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author zhangzhan
 * @email 52194440@qq.com
 * @date 2024-08-28 22:07:06
 */
@Api("属性分组")
@RestController
@RequestMapping("/applet/appletattrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private SupplierProductCategoryService supplierProductCategoryService;

    /**
     * 获取分类下所有分组&关联属性
     */
    @RequestMapping(value = "/{catelogId}/withattr", method = RequestMethod.GET)
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    @SystemLog(info = "获取分类下所有分组&关联属性")
    @ApiOperation(value = "获取分类下所有分组&关联属性", notes = NotesConstant.DEV)
    public ResponseResult getClassifyTheAssociatedGroupingAndGroupingAttributes(@PathVariable("catelogId") Long catelogId) {
        return ResponseResult.okResult(attrGroupService.getClassifyTheAssociatedGroupingAndGroupingAttributes(catelogId));
    }

    /**
     * 添加属性与分组关联关系
     */
    @RequestMapping(value = "/attr/relation", method = RequestMethod.POST)
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    @ApiOperation(value = "添加属性与分组关联关系", notes = NotesConstant.DEV)
    @SystemLog(info = "添加属性与分组关联关系")
    public ResponseResult attrRelationS(@RequestBody List<AttrAttrGroupRelationVo> vos) {
        attrGroupService.attrRelationS(vos);
        return ResponseResult.ok();
    }

    /**
     * 获取属性分组没有关联的其他属性
     */
    @RequestMapping(value = "/{attrGroupId}/noattr/relation", method = RequestMethod.POST)
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    @ApiOperation(value = "获取属性分组没有关联的其他属性", notes = NotesConstant.DEV)
    @SystemLog(info = "获取属性分组没有关联的其他属性")
    public ResponseResult noattrRelation(@RequestBody Map<String, Object> params,
                                         @PathVariable("attrGroupId") Long attrGroupId) {
        PageUtils page = attrGroupService.noattrRelation(params, attrGroupId);

        return ResponseResult.okResultPage("page", page);
    }

    /**
     * 删除属性分组和属性的关联关系
     */
    @RequestMapping(value = "/attr/relation/delete", method = RequestMethod.DELETE)
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    @SystemLog(info = "删除属性分组和属性的关联关系")
    @ApiOperation(value = "删除属性分组和属性的关联关系", notes = NotesConstant.DEV)
    public ResponseResult attrRelationDelete(@RequestBody List<AttrAttrGroupRelationVo> vos) {
        attrGroupService.attrRelationDelete(vos);

        return ResponseResult.ok();
    }

    /**
     * 查询属性组下的所有属性
     */
    @RequestMapping(value = "/{attrGroupId}/attr/relation", method = RequestMethod.GET)
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    @SystemLog(info = "查询属性组下的所有属性")
    @ApiOperation(value = "查询属性组下的所有属性", notes = NotesConstant.DEV)
    public ResponseResult attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrVo> attrVos = attrGroupService.attrRelation(attrGroupId);

        return ResponseResult.okResult(attrVos);
    }

    /**
     * 查詢所有分類的屬性分組 或模糊查询
     */
    @RequestMapping(value = "/list/{catelogId}",method = RequestMethod.POST)
    @SystemLog(info = "查詢所有分類的屬性分組 或模糊查询")
    @ApiOperation(value = "查詢所有分類的屬性分組 或模糊查询", notes = NotesConstant.DEV)
    public ResponseResult list(@RequestParam Map<String, Object> params, //模糊搜索條件
                               @PathVariable("catelogId") Integer catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return ResponseResult.okResultPage("page", page);
    }

    /**
     * 获取属性分组详情
     */
    @RequestMapping(value = "/info/{attrGroupId}",method = RequestMethod.GET)
    //@RequiresPermissions("gulimallproduct:attrgroup:info")
    @SystemLog(info = "获取属性分组详情")
    @ApiOperation(value = "获取属性分组详情", notes = NotesConstant.DEV)
    public ResponseResult info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);


        //回显分类所有层级路径id
        Long[] catelogIdPath = supplierProductCategoryService.getCatelogIdPath(attrGroup.getCatelogId());

        attrGroup.setCatelogPath(catelogIdPath);
        return ResponseResult.okResult(attrGroup);
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation(value = "列表")
    @SystemLog(info = "属性分组列表")
    public ResponseResult list(@RequestBody Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return ResponseResult.okResultPage("page", page);
    }


    /**
     * 保存
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ApiOperation(value = "保存")
    @SystemLog(info = "属性分组保存")
    public ResponseResult save(@RequestBody AttrGroupEntity appletAttrGroup) {
        attrGroupService.save(appletAttrGroup);

        return ResponseResult.ok();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    @ApiOperation(value = "修改")
    @SystemLog(info = "属性分组修改")
    public ResponseResult update(@RequestBody AttrGroupEntity appletAttrGroup) {
        attrGroupService.updateById(appletAttrGroup);

        return ResponseResult.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除")
    @SystemLog(info = "属性分组删除")
    public ResponseResult delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return ResponseResult.ok();
    }

}
